package com.talkweb.ncfw.web.action;

import java.math.BigDecimal;
import java.util.List;

import com.talkweb.ncfw.component.rights.entity.Role;
import com.talkweb.ncfw.component.rights.entity.RoleMenu;
import com.talkweb.ncfw.component.rights.entity.RoleUser;
import com.talkweb.ncfw.component.rights.service.IRightManager;
import com.talkweb.ncfw.component.rights.service.IRoleManager;
import com.talkweb.ncfw.data.IDataMap;
import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.data.ProcessResult;
import com.talkweb.ncfw.exceptions.BizBaseException;
import com.talkweb.ncfw.exceptions.ViewBaseException;
import com.talkweb.ncfw.security.SecurityHelper;
import com.talkweb.ncfw.utils.AssertUtils;
import com.talkweb.ncfw.utils.ObjectUtils;
import com.talkweb.ncfw.utils.StringUtils;
import com.talkweb.ncfw.web.struts2.ExtGridBaseAction;
import com.talkweb.ncfw.web.utils.HttpServletUtils;

/**
 * <p>文件名称: RoleAction.java</p>
 * <p>文件描述: 本类描述</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2011-9-18</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class RoleAction extends ExtGridBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 849683947446951538L;
	
	private IRoleManager roleManager;
	private IRightManager rightManager;
	private Role role;

	public IRoleManager getRoleManager() {
		return roleManager;
	}

	public void setRoleManager(IRoleManager roleManager) {
		this.roleManager = roleManager;
	}

	public IRightManager getRightManager() {
		return rightManager;
	}

	public void setRightManager(IRightManager rightManager) {
		this.rightManager = rightManager;
	}
	
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-18
	 * @Description：角色信息录入
	 * @return
	 * @throws ViewBaseException
	 */
	public String inputRole () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		if (ObjectUtils.isEmpty(role.getUserroleId())) {//新增
			try {
				int returnCode = this.roleManager.addRole(role);
				flag = (returnCode == 1) ? true : false;
				msg = flag ? "角色新增成功" : "角色新增失败";
 			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
		} else {
			try {
				int returnCode = this.roleManager.updateRole(role);
				flag = (returnCode == 1) ? true : false;
				msg = flag ? "角色新增成功" : "角色新增失败";
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-18
	 * @Description：删除角色信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String deleteRole () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		String roleIds 			= this.getParameter("roleIds");
		AssertUtils.notEmpty(roleIds, "roleIds can not be null");
		String[] roleIdArray	= roleIds.split(",");
		if (roleIdArray == null || roleIdArray.length == 0 || roleIdArray[0].trim().length() == 0) {
			AssertUtils.notEmpty(roleIds, "roleIds can not be null");
		}
		try {
			int returnCode = this.roleManager.deleteRoleByRoleId(roleIdArray);
			flag = (returnCode == 1) ? true : false;
			msg = flag ? "角色删除成功" : "角色删除失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-18
	 * @Description：查询角色列表
	 * @return
	 */
	public String listRole () throws ViewBaseException {
		IDataMap<String, Object> condition = this.getParameterMap();
		if (!SecurityHelper.hasSysadminRight()) {
			String[] roles = SecurityHelper.getCurrentUserRoles();
			condition.put("currentTransferRoleIdArray", roles);
		}
		Page<Role> page = this.getPage();
		try {
			page = this.roleManager.findRole(page, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			this.bindDataGrid(page);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2012-1-6
	 * @Description：查询角色
	 * @return
	 * @throws ViewBaseException
	 */
	public String findRole () throws ViewBaseException {
		IDataMap<String, Object> condition = this.getParameterMap();
		if (!SecurityHelper.hasSysadminRight()) {
			String[] roles = SecurityHelper.getCurrentUserRoles();
			condition.put("currentTransferRoleIdArray", roles);
		}
		Page<Role> page = this.getPage();
		try {
			page = this.roleManager.findRole(page, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJsonArray(this.getResponse(), page.getResult());
		}
		return null;
	} 
	
	/**
	 * 
	 * @author：Wuqingming
	 * @date：2011-9-28
	 * @Description：获取角色信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String getRoleByRoleId () throws ViewBaseException {
		String roleId = this.getParameter("roleId");
		Role role = null;
		try {
			role = this.roleManager.getRoleByRoleId(roleId);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), role);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-7
	 * @Description：根据角色查询角色菜单关系
	 * @return 
	 * @throws ViewBaseException
	 */
	public String findRoleMenuByRoleId () throws ViewBaseException {
		String roleId = this.getParameter("roleId");
		List<RoleMenu> roleMenuList = null;
		try {
			roleMenuList = this.rightManager.findRoleMenuByRoleId(roleId);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJsonArray(this.getResponse(), roleMenuList);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-7
	 * @Description：保存角色权限
	 * @return
	 * @throws ViewBaseException
	 */
	public String saveRoleAuthData () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		String roleId = this.getParameter("roleId");		//角色ID
		String resIds = this.getParameter("resIds");		//资源ID
		String resType = this.getParameter("resType");		//资源类型
		String[] resIdArray = null;
		if (StringUtils.isNotEmpty(resIds)) {
			resIdArray = resIds.split(",");
		}
		if (StringUtils.equals(resType, "menu")) {		//菜单
			try {
				int returnCode = this.rightManager.saveRoleMenu(roleId, resIdArray);
				flag = (returnCode == 1) ? true : false;
				msg = flag ? "角色菜单权限保存成功" : "角色菜单权限保存失败";
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-9
	 * @Description：删除角色用户关系
	 * @return
	 * @throws ViewBaseException
	 */
	public String deleteRoleUser () throws ViewBaseException {
		String roleId = this.getParameter("roleId");
		String userIds = this.getParameter("userIds");
		/*if (StringUtils.isEmpty(userIds)) {
			ProcessResult result = new ProcessResult(false, "用户编号不能为空.");
			if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
				HttpServletUtils.outJson(this.getResponse(), result);
			}
			return null;
		}*/
		String[] userIdArray = userIds.split(",");
		BigDecimal _roleId = new BigDecimal(roleId);
		RoleUser[] roleUsers = new RoleUser[userIdArray.length];
		int index = 0;
		for (String userId : userIdArray) {
			RoleUser roleUser = new RoleUser();
			roleUser.setUserId(userId);
			roleUser.setUserrolecolumn1(_roleId);
			roleUsers[index++] = roleUser;
		}
		boolean flag = false;
		String msg = null;
		try {
			int returnCode = this.rightManager.deleteRoleUser(roleUsers);
			flag = (returnCode == 1) ? true : false;
			msg = flag ? "角色用户信息删除成功" : "角色用户信息删除失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-9
	 * @Description：添加角色用户关系
	 * @return
	 * @throws ViewBaseException
	 */
	public String addRoleUser () throws ViewBaseException {
		String roleId = this.getParameter("roleId");
		String userIds = this.getParameter("userIds");
		/*if (StringUtils.isEmpty(userIds)) {
			ProcessResult result = new ProcessResult(false, "用户编号不能为空.");
			if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
				HttpServletUtils.outJson(this.getResponse(), result);
			}
			return null;
		}*/
		String[] userIdArray = userIds.split(",");
		BigDecimal _roleId = new BigDecimal(roleId);
		RoleUser[] roleUsers = new RoleUser[userIdArray.length];
		int index = 0;
		for (String userId : userIdArray) {
			RoleUser roleUser = new RoleUser();
			roleUser.setUserId(userId);
			roleUser.setUserrolecolumn1(_roleId);
			roleUsers[index++] = roleUser;
		}
		boolean flag = false;
		String msg = null;
		try {
			int returnCode = this.rightManager.addRoleUser(roleUsers);
			flag = (returnCode == 1) ? true : false;
			msg = flag ? "角色用户信息添加成功" : "角色用户信息添加失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
}

